package com.youlai.boot.modules.warehouse.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.warehouse.converter.WarehouseConverter;
import com.youlai.boot.modules.warehouse.mapper.WarehouseMapper;
import com.youlai.boot.modules.warehouse.model.Warehouse;
import com.youlai.boot.modules.warehouse.model.WarehouseForm;
import com.youlai.boot.modules.warehouse.model.WarehouseVO;
import com.youlai.boot.modules.warehouse.service.WarehouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.youlai.boot.modules.warehouse.model.ProductWarehousePageQuery;
import com.youlai.boot.modules.product.mapper.ProductMapper;
import com.youlai.boot.modules.product.model.Product;
import com.youlai.boot.common.exception.BusinessException;

@Service
@RequiredArgsConstructor
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    private final WarehouseConverter warehouseConverter;
    private final ProductMapper productMapper;
    private final WarehouseMapper warehouseMapper;

    @Override
    public Page<WarehouseVO> getWarehousePage(String name, int pageNum, int pageSize) {
        Page<WarehouseVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.getWarehousePage(page, name);
    }

    @Override
    public WarehouseVO getWarehouseById(Long id) {
        Warehouse warehouse = this.getById(id);
        return warehouseConverter.toVO(warehouse);
    }

    @Override
    public Long saveWarehouse(WarehouseForm form) {
        Warehouse warehouse = warehouseConverter.toEntity(form);
        this.save(warehouse);
        return warehouse.getId();
    }

    @Override
    public void updateWarehouse(WarehouseForm form) {
        Warehouse warehouse = warehouseConverter.toEntity(form);
        this.updateById(warehouse);
    }

    @Override
    public boolean deleteWarehouse(Long id) {
        return this.removeById(id);
    }

    @Override
    public Page<WarehouseVO> getProductWarehousePage(Long productId, ProductWarehousePageQuery query) {
        Page<WarehouseVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return warehouseMapper.getProductWarehousePage(page, productId);
    }

    @Override
    public boolean saveProductWarehouse(Long productId, Long warehouseId) {
        // 检查商品是否存在
        Product product = productMapper.selectById(productId);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }

        // 检查仓储公司是否存在
        Warehouse warehouse = warehouseMapper.selectById(warehouseId);
        if (warehouse == null) {
            throw new BusinessException("仓储公司不存在");
        }

        // 检查商品是否已关联仓储公司
        boolean exists = warehouseMapper.checkProductWarehouseExists(productId, warehouseId);
        if (exists) {
            throw new BusinessException("该商品已关联此仓储公司");
        }
        
        return warehouseMapper.saveProductWarehouse(productId, warehouseId);
    }

    @Override
    public boolean deleteProductWarehouse(Long productId, Long warehouseId) {
        return warehouseMapper.deleteProductWarehouse(productId, warehouseId);
    }    
}